home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Picking Mesh ShapeParts / Sources / PickMeshShapePartShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-21  |  9.9 KB  |  420 lines  |  [TEXT/CWIE]

  1. // PickMeshShapePartShell.c
  2. //
  3. // This is MeshShapePartPick, an adaptation of box, the QuickDraw 3D starter program,
  4. // written by Nick Thompson.
  5. // modifed by Robert Dierkes for an example of mesh shape part picking.
  6. // 
  7. // Â©1994-95 Apple computer Inc., All Rights Reserved
  8. //
  9.  
  10. // system headers
  11. #include <Fonts.h>
  12. #include <Menus.h>
  13. #include <DiskInit.h>
  14. #include <ToolUtils.h>
  15.  
  16.  
  17. #include "PickMeshShapePart.h"
  18. #include "PickMeshShapePartSupport.h"
  19.  
  20.  
  21. //-------------------------------------------------------------------------------------------
  22. // function prototypes
  23.  
  24. void InitDocumentData(
  25.     DocumentPtr    theDocument);
  26.  
  27. void DisposeDocumentData(
  28.     DocumentPtr    theDocument);
  29.  
  30. void InitToolbox(
  31.     void);
  32.  
  33. void InitMenuItems(
  34.     DocumentPtr    theDocument);
  35.  
  36. void MainEventLoop(
  37.     void);
  38.  
  39. void HandleKeyPress(
  40.     EventRecord    *event);
  41.  
  42. void HandleMenuCommand(
  43.     long    result);
  44.  
  45.  
  46. //-------------------------------------------------------------------------------------------
  47. //
  48.  
  49. Boolean         gQuitFlag     = false;
  50. WindowPtr        gMainWindow    = nil;
  51. DocumentRec        gDocument;
  52.  
  53.  
  54. //-------------------------------------------------------------------------------------------
  55. // main()
  56. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  57. // and enter the main event loop.  On exit from the main event loop, we want to call
  58. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  59.  
  60. void main(
  61.     void)
  62. {
  63.     Rect    windowBounds = { 50, 50, 350, 350 };
  64.     Str255    title = "\pMesh ShapePart Picking";
  65.  
  66.     InitToolbox();
  67.  
  68.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  69.     if(Q3Initialize() == kQ3Failure) {
  70.         DebugStr("\pErInitialize returned failure.");
  71.         return;
  72.     }
  73.  
  74.     // set up our globals
  75.     gQuitFlag  = false;
  76.     gMainWindow = NewCWindow(nil, &windowBounds, title, true, noGrowDocProc, (WindowPtr)-1, true, 0);
  77.     InitDocumentData(&gDocument);
  78.     InitMenuItems(&gDocument);
  79.     InitPicking();
  80.  
  81.     MainEventLoop();
  82.     
  83.     ExitPicking();
  84.     DisposeDocumentData(&gDocument);
  85.     
  86.     //    Close our connection to the QuickDraw 3D library
  87.     if(Q3Exit() == kQ3Failure)
  88.         DebugStr("\pErExit returned failure.");
  89. }
  90.  
  91. //-------------------------------------------------------------------------------------------
  92. //
  93.  
  94. static
  95. void InitDocumentData(
  96.     DocumentPtr theDocument) 
  97. {
  98.     // sets up the 3d data for the scene
  99.     // Create view for QuickDraw 3D.
  100.     theDocument->fView = MyNewView((WindowPtr)gMainWindow);
  101.  
  102.     // the main display group:
  103.     theDocument->fModel = MyNewModel();
  104.  
  105.     // the drawing styles:
  106.     theDocument->fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleVertex);
  107.     theDocument->fBackFacing    = Q3BackfacingStyle_New(kQ3BackfacingStyleBoth);
  108.     theDocument->fFillStyle     = Q3FillStyle_New(kQ3FillStyleFilled);
  109.     theDocument->fPickPartStyle = Q3PickPartsStyle_New(theDocument->fPickParts);
  110.  
  111.     theDocument->fPickParts = kQ3PickPartsObject;
  112.  
  113.     // Add fPickPartStyle to the model as the first object before the geometry
  114.     if (theDocument->fPickPartStyle != NULL) {
  115.         TQ3GroupPosition    firstPosition;
  116.  
  117.         if (Q3Group_GetFirstPosition(theDocument->fModel, &firstPosition) == kQ3Success  &&
  118.             firstPosition != NULL) {
  119.  
  120.             // Add the fPickPartStyle to the beginning of the fModel group
  121.             Q3Group_AddObjectBefore(theDocument->fModel, firstPosition, theDocument->fPickPartStyle);
  122.  
  123.             // Dispose our 2nd reference to the style now that it's added
  124.             // to the group but this reference can still be used when the
  125.             // application wants to set pick parts style other part values.
  126.             Q3Object_Dispose(theDocument->fPickPartStyle);
  127.         }
  128.     }
  129. }
  130.  
  131.  
  132. static
  133. void DisposeDocumentData(
  134.     DocumentPtr theDocument)
  135. {
  136.     Q3Object_Dispose(theDocument->fView);            // the view for the scene
  137.     Q3Object_Dispose(theDocument->fModel);            // object in the scene being modelled
  138.     Q3Object_Dispose(theDocument->fInterpolation);    // interpolation style used when rendering
  139.     Q3Object_Dispose(theDocument->fBackFacing);        // whether to draw shapes that face away from the camera
  140.     Q3Object_Dispose(theDocument->fFillStyle);        // whether drawn as solid filled object or decomposed to components
  141.     // No need to delete fPickPartStyle; its only reference is in the fModel
  142. }
  143.  
  144. //-----------------------------------------------------------------------------
  145. // 
  146.  
  147. TQ3Status DrawDocumentData(
  148.     DocumentPtr theDocument)
  149. {    
  150.     Q3View_StartRendering(theDocument->fView);
  151.     do {
  152.         Q3Style_Submit(theDocument->fInterpolation,    theDocument->fView);
  153.         Q3Style_Submit(theDocument->fBackFacing,    theDocument->fView);
  154.         Q3Style_Submit(theDocument->fFillStyle,        theDocument->fView);
  155.         Q3DisplayGroup_Submit(theDocument->fModel,    theDocument->fView);
  156.     } while(Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse);
  157.     return kQ3Success;
  158. }
  159.  
  160. //-------------------------------------------------------------------------------------------
  161. //
  162.  
  163. static
  164. void InitToolbox(
  165.     void)
  166. {
  167.     MaxApplZone();
  168.     MoreMasters(); MoreMasters(); MoreMasters(); 
  169.     
  170.     InitGraf(&qd.thePort);
  171.     InitFonts();
  172.     InitWindows();
  173.     InitCursor();
  174.     InitMenus();
  175.  
  176.     FlushEvents(everyEvent, 0);
  177.     // initialize application globals
  178.     
  179.     gQuitFlag = false;
  180. }
  181.  
  182.  
  183. static
  184. void InitMenuItems(
  185.     DocumentPtr    theDocument)
  186. {
  187.     MenuHandle    hMenu;
  188.  
  189.     /* Set up menus */
  190.     hMenu = NewMenu(mApple, "\p\024");
  191.     AppendMenu(hMenu, "\pAbout Mesh ShapePart Picking…");
  192.     AppendMenu(hMenu, "\p(-");
  193.     AddResMenu(hMenu, 'DRVR');
  194.     InsertMenu(hMenu, 0);
  195.  
  196.     hMenu = NewMenu(mFile, "\pFile");
  197.     AppendMenu(hMenu, "\pQuit/Q");
  198.     InsertMenu(hMenu, 0);
  199.  
  200.     hMenu = NewMenu(mParts, "\pShapeParts");
  201.     AppendMenu(hMenu, "\pObject/O;Face/F;Edge/E;Vertex/V");
  202.     InsertMenu(hMenu, 0);
  203.  
  204.     DrawMenuBar();
  205.  
  206.     /* Initialize shape part menu */
  207.     CheckItem(hMenu, iObject,    (Boolean)  (theDocument->fPickParts == kQ3PickPartsObject));
  208.     CheckItem(hMenu, iFace,        (Boolean) ((theDocument->fPickParts & kQ3PickPartsMaskFace)   != 0));
  209.     CheckItem(hMenu, iEdge,        (Boolean) ((theDocument->fPickParts & kQ3PickPartsMaskEdge)   != 0));
  210.     CheckItem(hMenu, iVertex,    (Boolean) ((theDocument->fPickParts & kQ3PickPartsMaskVertex) != 0));
  211. }
  212.  
  213.  
  214. //-------------------------------------------------------------------------------------------
  215. //
  216. static
  217. void MainEventLoop(
  218.     void)
  219. {
  220.     EventRecord     event;
  221.     WindowPtr       window;
  222.     short           thePart;
  223.     Rect            screenRect, updateRect;
  224.     Point            aPoint = {100, 100};
  225.     
  226.     while(!gQuitFlag)
  227.     {
  228.         if(WaitNextEvent(everyEvent, &event, 0, nil))
  229.         {
  230.  
  231.             switch (event.what) {
  232.                 case mouseDown:
  233.                 
  234.                     thePart = FindWindow(event.where, &window);
  235.                     
  236.                     switch (thePart) {
  237.                         case inMenuBar:
  238.                             HandleMenuCommand(MenuSelect(event.where));
  239.                             break;
  240.  
  241.                         case inDrag:
  242.                             screenRect =(**GetGrayRgn()).rgnBBox;
  243.                             DragWindow(window, event.where, &screenRect);
  244.                             break;
  245.                     
  246.                         case inContent:
  247.                             if(window != FrontWindow())
  248.                                 SelectWindow(window);
  249.                             else {
  250.                                 CGrafPtr    savedPort;
  251.                                 Point        clickPoint;
  252.  
  253.                                 GetPort((GrafPtr*)&savedPort);
  254.                                 SetPort(window);
  255.                                 clickPoint = event.where;
  256.                                 GlobalToLocal(&clickPoint);
  257.                                 SetPort((GrafPtr)savedPort);
  258.  
  259.                                 DoPicking(&clickPoint, &gDocument);
  260.                             }
  261.                             break;
  262.                     
  263.                         case inGoAway:
  264.                             if(TrackGoAway(window, event.where)) {
  265.                                 DisposeWindow(window);
  266.                                 gQuitFlag = true;
  267.  
  268.                             }
  269.                             break;
  270.  
  271.                         default:
  272.                             break;
  273.                     }
  274.                     break;
  275.  
  276.  
  277.                 case updateEvt:
  278.                     window =(WindowPtr)event.message;
  279.                     updateRect =(**(window->visRgn)).rgnBBox;
  280.                     SetPort(window);
  281.                     BeginUpdate(window);
  282.                     DrawDocumentData(&gDocument);
  283.                     EndUpdate(window);
  284.                     break;
  285.                     
  286.                 case keyDown:
  287.                 case autoKey:
  288.                     HandleKeyPress(&event);
  289.                     break;
  290.                     
  291.                 case diskEvt:
  292.                     if(HiWord(event.message) != noErr) 
  293.                         (void) DIBadMount(aPoint, event.message);
  294.                     break;
  295.                     
  296.                 case osEvt:
  297.                 case activateEvt:
  298.                     break;
  299.             }
  300.         }
  301.     }
  302. }
  303.  
  304.  
  305. //-------------------------------------------------------------------------------------------
  306. //
  307. static
  308. void HandleKeyPress(
  309.     EventRecord *event)
  310. {
  311.     char    charCode;
  312.  
  313.     charCode = event->message & charCodeMask;
  314.  
  315.     if(event->modifiers & cmdKey)
  316.         HandleMenuCommand(MenuKey(charCode));
  317. }
  318.  
  319.  
  320. //-------------------------------------------------------------------------------------------
  321. //
  322. static
  323. void HandleMenuCommand(
  324.     long menuResult)
  325. {
  326.     short    menuID,
  327.             itemNumber;
  328.  
  329.     if(menuResult == 0)
  330.         return;
  331.  
  332.     menuID       = HiWord(menuResult);
  333.     itemNumber = LoWord(menuResult);
  334.  
  335.     switch (menuID) {
  336.         case mApple:
  337.             switch (itemNumber) {
  338.                 case iAbout:
  339.                     SysBeep(1);
  340.                     break;
  341.  
  342.                 default:
  343.                     break;
  344.             }
  345.             break;
  346.  
  347.         case mFile:
  348.             switch (itemNumber) {
  349.                 case iQuit:
  350.                     gQuitFlag = true;
  351.                     break;
  352.  
  353.                 default:
  354.                     break;
  355.             }
  356.             break;
  357.  
  358.         case mParts:
  359.             {
  360.                 MenuHandle    hMenu;
  361.  
  362.                 hMenu = GetMenuHandle(menuID);
  363.  
  364.                 switch (itemNumber) {
  365.                     case iObject:
  366.                         gDocument.fPickParts = kQ3PickPartsObject;
  367.                         CheckItem(hMenu, iFace,   false);
  368.                         CheckItem(hMenu, iEdge,   false);
  369.                         CheckItem(hMenu, iVertex, false);
  370.                         break;
  371.     
  372.                     case iFace:
  373.                         if(gDocument.fPickParts & kQ3PickPartsMaskFace) {
  374.                             gDocument.fPickParts &= ~kQ3PickPartsMaskFace;
  375.                             CheckItem(hMenu, itemNumber, false);
  376.                         }
  377.                         else {
  378.                             gDocument.fPickParts |= kQ3PickPartsMaskFace;
  379.                             CheckItem(hMenu, itemNumber, true);
  380.                         }
  381.                         break;
  382.     
  383.                     case iEdge:
  384.                         if(gDocument.fPickParts & kQ3PickPartsMaskEdge) {
  385.                             gDocument.fPickParts &= ~kQ3PickPartsMaskEdge;
  386.                             CheckItem(hMenu, itemNumber, false);
  387.                         }
  388.                         else {
  389.                             gDocument.fPickParts |= kQ3PickPartsMaskEdge;
  390.                             CheckItem(hMenu, itemNumber, true);
  391.                         }
  392.                         break;
  393.     
  394.                     case iVertex:
  395.                         if(gDocument.fPickParts & kQ3PickPartsMaskVertex) {
  396.                             gDocument.fPickParts &= ~kQ3PickPartsMaskVertex;
  397.                             CheckItem(hMenu, itemNumber, false);
  398.                         }
  399.                         else {
  400.                             gDocument.fPickParts |= kQ3PickPartsMaskVertex;
  401.                             CheckItem(hMenu, itemNumber, true);
  402.                         }
  403.                         break;
  404.     
  405.                     default:
  406.                         break;
  407.                 }
  408.     
  409.                 /* Un/check "object" item after other part menu items are selected */
  410.                 CheckItem(hMenu, iObject, (Boolean) (gDocument.fPickParts == kQ3PickPartsObject));
  411.             }
  412.             break;
  413.  
  414.         default:
  415.             break;
  416.     }
  417.  
  418.     HiliteMenu(0);
  419. }
  420.